﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace PT
{
    public partial class Layout : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((bool)Session["logged"])
            {
                cpLogged.Visible = true;
                cpNotLogged.Visible = false;
                RemoveOldReservations();
            }
            else
            {
                cpLogged.Visible = false;
                cpNotLogged.Visible = true;
            }
        }

        protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
        {
            Session["logged"] = false;
        }

        protected void RemoveOldReservations()
        {
            //string connString = @"Data Source=MDX-PC\SQLEXPRESS;Initial Catalog=Hotel;Integrated Security=True";
            string connString = ConfigurationManager.ConnectionStrings["HotelConnectionString1"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand selectRoomReservations = conn.CreateCommand();
            selectRoomReservations.CommandText = "SELECT id, endDate FROM Reservation WHERE endDate < GETDATE() ORDER BY startDate ASC";
            SqlCommand deleteRoomReservations = conn.CreateCommand();
            deleteRoomReservations.CommandText = "DELETE Reservation WHERE id = @ReservationId";
            deleteRoomReservations.Parameters.Add(new SqlParameter("@ReservationId", SqlDbType.Int, 4, "id"));
            conn.Open();
            DataSet resDS = new DataSet();
            SqlDataAdapter resDA = new SqlDataAdapter(selectRoomReservations);
            resDA.DeleteCommand = deleteRoomReservations;
            resDA.Fill(resDS);
            DataTable resTable = resDS.Tables[0];
            int index = resTable.Rows.Count;
            while(Convert.ToBoolean(index))
            {
                index--;
                resTable.Rows[index].Delete();
            }
            resDA.Update(resDS);
        }
    }
}
